<?php 
class Taak extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('form');
		$this->load->helper('url');
		$this->load->library('form_validation');

	}
	
	public function index(){
		$this->showTaken();
	}	
	
	public function addTaak(){
		$clientid= $this->uri->segment(4);
		if($this->validationRules()) {
			if ($this->form_validation->run() == FALSE){
				$this->showTaken();
			}
			else {
				$this->load->model('taak_model');
				$this->taak_model->newTaak();
				if($this->uri->segment(6) === FALSE){
					redirect(base_url().'index.php/client/gotoClientProfile/id/'.$clientid, 'refresh');
				}
				else {
					redirect(base_url().'index.php/medewerker/gotoMedewerkerProfile/id/'.$this->uri->segment(6), 'refresh');
				}
			}
		}
	}
	
	public function verwijderTaak(){
		$clientid = $this->uri->segment(6);
		$taakid = $this->uri->segment(4);
		$this->load->model('taak_model');
		$this->taak_model->deleteTaak($taakid);
		if($this->uri->segment(8) === FALSE){
			redirect(base_url().'index.php/client/gotoClientProfile/id/'.$clientid, 'refresh');
		}
		else {
			redirect(base_url().'index.php/medewerker/gotoMedewerkerProfile/id/'.$this->uri->segment(8), 'refresh');
		}
	}
	
	public function editTaak(){
		$this->load->model('taak_model');
		$this->load->model('medewerker_model');
		$this->load->model('client_model');
		$data['medewerker'] = $this->medewerker_model->getMedewerkers();
		$data['clienten'] = $this->client_model->getClients();
		$data['query2'] = $this->taak_model->getTaak($this->uri->segment(4));
		$data['query'] = $this->taak_model->getTakenByMedewerker(1);
		$title['title'] = 'ICB Digitale thuiszorg documentatie systeem';
		$title['slogan'] = 'Maakt thuiszorg eenvoudiger en geeft de mogelijkheid om digitaal een archief te onderhouden.';
		$this->load->view('includes/header', $title);
		$this->load->view('taken_views/edit_taak', $data);
	}
	
	public function updateTaak(){
		$clientid = $this->uri->segment(6);
		
		if($this->validationRules()) {
			if ($this->form_validation->run() == FALSE){
				$this->editTaak();
			}
			else {
				$this->load->model('taak_model');
				$this->taak_model->updateTaak($this->uri->segment(4));
				if($this->uri->segment(8) === FALSE){
					redirect(base_url().'index.php/client/gotoClientProfile/id/'.$clientid, 'refresh');
				}
				else {
					redirect(base_url().'index.php/medewerker/gotoMedewerkerProfile/id/'.$this->uri->segment(8), 'refresh');
				}
			}
		
		}
	}
	
	
	public function showTaken(){
		$this->load->model('client_model');
		$this->load->model('taak_model');
		$this->load->model('medewerker_model');
		$data['clienten'] = $this->client_model->getClients();
		$data['medewerker'] = $this->medewerker_model->getMedewerkers();
		$data['query2'] = $this->taak_model->getTaak($this->uri->segment(4));
		$data['query'] = $this->taak_model->getTakenByMedewerker(1);
		$title['title'] = 'ICB Digitale thuiszorg documentatie systeem';
		$title['slogan'] = 'Maakt thuiszorg eenvoudiger en geeft de mogelijkheid om digitaal een archief te onderhouden.';
		$this->load->view('includes/header', $title);
		$this->load->view('taken_views/add_taak', $data);
	}
	
	private function validationRules(){
		$this->form_validation->set_rules('aantal_minuten', 'Aantal Minuten', 'required|numeric');
		$this->form_validation->set_rules('beschrijving', 'Taak beschrijving', 'required');
		$this->form_validation->set_rules('yadayadadatepicker', 'Datum', 'required');
		$this->form_validation->set_rules('onderwerp', 'Onderwerp', 'required');
		
		return true;
	}
}
?>